<?php
/**
 * Created by PhpStorm.
 * User: huliren<huliren0516@163.com>
 * Date: 2017/9/6
 * Time: 11:45
 */

define('SITE_AUTH', 'user-gold-auditor');
require('../../../common.inc.php');

use lib\core\AdminAction;
use Center\Payment;
use Center\CashLog as Model;
use Mobile\AppPush;
use Center\User as CUser;

class goldAuditor extends AdminAction
{
    private $status = [
        1 => '提现中',
        2 => '提现完成',
        3 => '提现失败',
    ];

    public function doDefault()
    {
        $smarty = $this->app->page();

        $where = ['Type' => 4];
        $userid = (isset($_GET['s_userid']) and $_GET['s_userid'] !== '') ? trim(intval($_GET['s_userid'])) : null;
        if ($userid !== null) {
            $where += ['UserId' => $userid];
        }
        $status = (isset($_GET['s_status']) and $_GET['s_status'] !== '') ? intval($_GET['s_status']) : null;
        if ($status !== null) {
            $where += ['Status' => $status];
        }
        $start_time = (isset($_GET['start_time']) and $_GET['start_time'] !== '') ? $_GET['start_time'] : null;
        if ($start_time !== null) {
            $where += ['CreateTime >=' => $start_time];
        }
        $end_time = (isset($_GET['end_time']) and $_GET['end_time'] !== '') ? $_GET['end_time'] : null;
        if ($end_time !== null) {
            $where += ['CreateTime <=' => $end_time];
        }
        $s_data= [
            's_userid'=>$userid,
            's_status'=>$status,
            's_start_time'=>$start_time,
            's_end_time'=>$end_time,
        ];

        if (!empty($_GET['exportExcel'])) {
            $data = Model::getList($where, 0, 0);
            set_time_limit(0);
            if (!empty($data['data'])) {
                $user_id = [];
                foreach ($data['data'] as $k => $v) {
                    $user_id[$v['UserId']] = $v['UserId'];
                }
                $user_data = CUser::getDataListByIds($user_id);
                $user = [];
                foreach ($user_data as $k => $v) {
                    $user[$v['UserId']] = $v['NickName'];
                }
            }
            $filename = mb_convert_encoding('用户提现审核-' . date("YmdHis"), "GB2312", "UTF-8");
            $filename_type = 'xls';
            header("Content-type:application/vnd.ms-excel");
            Header("Accept-Ranges:bytes");
            Header("Content-Disposition:attachment;filename=" . $filename . "." . $filename_type);  //$filename导出的文件名
            header("Pragma: no-cache");
            header("Expires: 0");
            $excel_str = '';
            if ($filename_type == 'xls') {
                $excel_str = <<<EOT
            <html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40">
             <head>
              <meta http-equiv="expires" content="Mon, 06 Jan 1999 00:00:01 GMT">
              <meta http-equiv=Content-Type content="text/html; charset=gb2312">
              <!--[if gte mso 9]><xml>
              <x:ExcelWorkbook>
              <x:ExcelWorksheets>
                <x:ExcelWorksheet>
                <x:Name></x:Name>
                <x:WorksheetOptions>
                  <x:DisplayGridlines/>
                </x:WorksheetOptions>
                </x:ExcelWorksheet>
              </x:ExcelWorksheets>
              </x:ExcelWorkbook>
              </xml><![endif]-->
             </head>
EOT;
            }
            $excel_str .= '<table>';
            $excel_str .= '<tr><td>用户ID</td><td>昵称</td><td>提取现金数(元)</td><td>创建时间</td><td>审核时间</td><td>审核状态</td></tr>';
            foreach ($data['data'] as $k => $v) {
                $excel_str .= "<tr><td style='vnd.ms-excel.numberformat:@'>{$v['UserId']}</td><td>{$user[$v['UserId']]}</td><td>{$v['Num']}</td><td>{$v['CreateTime']}</td><td>{$v['UpdateTime']}</td><td>{$this->status[$v['Status']]}</td></tr>";
            }
            $excel_str .= '</table>';
            echo mb_convert_encoding($excel_str,"GB2312","UTF-8");
            exit();
        }

        $page = $_GET['page'] < 1 ? 1 : (int)$_GET['page'];
        $limit = parent::PAGE_SIZE;
        $offset = ($page - 1) * $limit;

        $data = Model::getList($where, $offset, $limit);
        $count = $data['count'];

        if ($count > 0) {
            $page_info['page'] = ceil($count / $limit);
            $page_info['count'] = $count;
            if (!empty($data['data'])) {
                $user_id = [];
                foreach ($data['data'] as $k => $v) {
                    $user_id[$v['UserId']] = $v['UserId'];
                }
                $user_data = CUser::getDataListByIds($user_id);
                $user = [];
                foreach ($user_data as $k => $v) {
                    $user[$v['UserId']] = $v['NickName'];
                }
                $smarty->value('user', $user);
            }
            $pagination = $this->pagination($count, $limit, $page);
            $smarty->value('page_info', $page_info);
            $smarty->value('pagination', $pagination);
            $smarty->value('list', $data['data']);
        }
        $smarty->value('status', $this->status);
        $smarty->value('s_data',$s_data);
        $smarty->output('gold/goldAuditor_list.tpl');
    }

    public function doApproval()
    {
        $CashLogId = $_POST['id'];
        $userid = $_POST['userid'];
        $rel = Payment::withdraw($CashLogId);
        if ($rel) {
            AppPush::pushByUserId($userid,AppPush::PUSH_TYPE_Wallet,'您的提现审核完成，转账成功！');
            $this->output(['type' => 'yes', 'date' => $rel]);
        } else {
            $this->output(['type' => 'no']);
        }

    }
}

$app->run();